package pers.qianyu.month_202012.date_20201231;

import org.junit.*;

/**
 * 115. 不同的子序列
 * https://leetcode-cn.com/problems/distinct-subsequences/
 *
 * @author mizzle rain
 * @date 2020-12-31 20:02
 */
public class NumDistinct {
    public int numDistinct(String s, String t) {
        int lenS = s.length();
        int lenT = t.length();
        int[][] dp = new int[lenS + 1][lenT + 1];
        for (int i = 0; i <= lenS; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i <= lenS; i++) {
            for (int j = 1; j <= lenT; j++) {
                if (s.charAt(i - 1) != t.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                }
            }
        }
        return dp[lenS][lenT];
    }

    @Test
    public void test1() {
        String s = "rabbbit", t = "rabbit";
        int result = new NumDistinct().numDistinct(s, t);
        Assert.assertEquals(result, 3);
    }
}
